	package office.action.business;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import office.BusiUtil;
import office.action.business.form.BidBookConditionForm;
import office.action.business.form.BidBookForm;
import office.action.business.wrapper.BidBookWrapper;
import office.service.business.IBidBookService;
import platform.common.action.BaseAction;
import platform.common.bo.Page;
import current.security.user.domain.User;
import current.security.user.manager.service.UserManagerService;

/**
 * 
 * 标书管理
 * 
 * @author ZhangYQ
 * 
 */
public class BidBookAction extends BaseAction {
	private static final long serialVersionUID = 1L;
	private IBidBookService bidBookService;
	private Page page; // 分页信息
	private List<BidBookWrapper> wrapperList;//标书List
	private List<User> userList =new ArrayList<User>() ;
	private BidBookForm form;
	private BidBookConditionForm condition;
	private String id;
	@Resource
	private UserManagerService userManagerService;
	private List<User> checkUserList;
	private boolean check;
	private String status;
	private String loginUserId;

	/**
	 * 获取所有的标书以及根据条件进行查询
	 * 
	 * @return
	 * @throws Exception
	 */
	public String findAllBidBook() throws Exception {
		String resultString="listBidBookByPage";
		if(condition==null){
			resultString= "findAllBidBook";
			condition= new BidBookConditionForm();
		}
		if(id!=null){
			condition.setApplyMan(id);
		}else{
			condition.setApplyMan((String)getUserMap().get("userId"));
		}
		page = bidBookService.getPageInfo(condition);
		wrapperList = bidBookService.findAllBidBook(condition,page);
		if(BusiUtil.validateRole(getUserRoleName())){
			userList = userManagerService.getUsersByAllDepartment(getUserDepartmentId());
		}
		
		return resultString;
	}

	/**
	 * 初始化编辑标书页面
	 * @return
	 * @throws Exception
	 */
	public String gotoEditBidBook() throws Exception {
		if(null!=id&&!"".equals(id)){
			form = bidBookService.getById(id);
		}
		//checkUserList
		checkUserList = userManagerService.getUserByRoleAndCompany("cwjl",getUserCompanyId());
		//获取销售人员接口待实现
		userList=userManagerService.getUserByRoleAndCompany("xsry",getUserCompanyId());
		//获取审批人
		return "gotoEditBidBook";
	}

	/**
	 * 编辑标书
	 * 
	 * @return
	 * @throws Exception
	 */
	public String editBidBook() throws Exception {
		try {
			//获取当前用户
			String userId=(String) getUserMap().get("userId");
			form.setApplyMan(userId);
			String role = (String)getUserMap().get("roleName");
			form.setEntryRole(role);
			bidBookService.editBidBook(form);
			if (null == form.getId()
					|| ("").equals(form.getId())) {
				this.getAjaxMessagesJson().setMessage(true, "", "新建成功");
			} else {
				this.getAjaxMessagesJson().setMessage(true, "", "编辑成功");
			}

		} catch (Exception e) {
			this.handleException(e);
		}
		return RESULT_AJAXJSON;
	}
	/**
	 * 初始化标书费用申请
	 * @return
	 * @throws Exception
	 */
	public String gotoBidBookPriceApp() throws Exception {
		if(null!=id&&!"".equals(id)){
			form = bidBookService.getById(id);
		}
		//获取销售人员接口待实现
		userList=userManagerService.getUserByRoleAndCompany("swjl", getUserDepartmentId());//tUserByRole("swjl");
		//获取审批人
		return "gotoBidBookPriceApp";
	}
	
	/**
	 * 标书费用申请
	 * @return
	 * @throws Exception
	 */
	public String bidBookPriceApp() throws Exception {
		try {
			bidBookService.bidBookPriceApp(form);
			if (null == form.getId()
					|| ("").equals(form.getId())) {
				this.getAjaxMessagesJson().setMessage(true, "", "申请成功");
			} else {
				this.getAjaxMessagesJson().setMessage(true, "", "编辑成功");
			}

		} catch (Exception e) {
			this.handleException(e);
		}
		return RESULT_AJAXJSON;
	}	
	/**
	 * 
	 * <br/>
	 * 方法描述:分页条件查询产品结构
	 * 
	 * @return
	 * @throws Exception
	 */
	public String listBidBookByPage() throws Exception {
		try {
			if(condition.getApplyMan()==null){
				condition.setApplyMan((String)getUserMap().get("userId"));
			}
			wrapperList = bidBookService.findAllBidBook(condition, page);
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			throw e;
		}
		return "listBidBookByPage";
	}

	/**
	 * 初始化产品结构列表操作菜单
	 * 
	 * @return
	 * @throws Exception
	 */

	public String gotoBidBookMenu() throws Exception {
		form = bidBookService.getById(id);
		loginUserId=(String) getUserMap().get("userId");
		return "gotoBidBookMenu";
	}

	/**
	 * 跳转到标书删除确认页面
	 */
	public String gotoDeleteBidBook(){
		return "gotoDeleteBidBook";
	}
	
	/**
	 * 跳转到确定中标页面
	 */
	public String gotoBidBookBidStatus(){
		return "gotoBidBookBidStatus";
	}
	/**
	 * 跳转到确定中标页面
	 */
	public String bidBookBidStatus()throws Exception{
		try {
			bidBookService.bidBookBidStatus(id);
			this.getAjaxMessagesJson().setMessage(true, "", "操作成功");
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			throw e;
		}
		return RESULT_AJAXJSON;
	}
	
	/**
	 * 删除标书
	 * @return
	 * @throws Exception
	 */
	public String delBidBook() throws Exception {
		try {
			bidBookService.delBidBookById(id);
			this.getAjaxMessagesJson().setMessage(true, "", "操作成功");
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			throw e;
		}
		return RESULT_AJAXJSON;
	}
	/**
	 * 初始化审批标书
	 * @return
	 * @throws Exception
	 */
	public String gotocheckBidBook()throws Exception{
		return "gotoCheckBidBook";
	}
	
	/**
	 * 审批标书
	 * @return
	 * @throws Exception
	 */
	public String checkBidBook()throws Exception{
		try {
			bidBookService.checkBidBook(id, status);
			this.getAjaxMessagesJson().setMessage(true, "", "操作成功");
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			throw e;
		}
		return RESULT_AJAXJSON;
	}

	public IBidBookService getBidBookService() {
		return bidBookService;
	}

	public void setBidBookService(IBidBookService bidBookService) {
		this.bidBookService = bidBookService;
	}

	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}

	public List<BidBookWrapper> getWrapperList() {
		return wrapperList;
	}

	public void setWrapperList(List<BidBookWrapper> wrapperList) {
		this.wrapperList = wrapperList;
	}

	public BidBookForm getForm() {
		return form;
	}

	public void setForm(BidBookForm form) {
		this.form = form;
	}

	public BidBookConditionForm getCondition() {
		return condition;
	}

	public void setCondition(BidBookConditionForm condition) {
		this.condition = condition;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public List<User> getUserList() {
		return userList;
	}

	public void setUserList(List<User> userList) {
		this.userList = userList;
	}

	public UserManagerService getUserManagerService() {
		return userManagerService;
	}

	public void setUserManagerService(UserManagerService userManagerService) {
		this.userManagerService = userManagerService;
	}

	public List<User> getCheckUserList() {
		return checkUserList;
	}

	public void setCheckUserList(List<User> checkUserList) {
		this.checkUserList = checkUserList;
	}

	public boolean isCheck() {
		return check;
	}

	public void setCheck(boolean check) {
		this.check = check;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public String getLoginUserId() {
		return loginUserId;
	}

	public void setLoginUserId(String loginUserId) {
		this.loginUserId = loginUserId;
	}
	
}
